<?php

/*
=====================================================
 Generic Toolbox - by Carl Wells
-----------------------------------------------------
-----------------------------------------------------
 Copyright (c) 2009 Carl Wells
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
=====================================================
 File: system/display/adminpromodisplay.class.php
-----------------------------------------------------
 Purpose: This file builds all visual aspects of the Admin Section.
 All browser output is managed by this file.
=====================================================
*/
class Adminpromodisplay extends Admindisplay
{
             
      function displayPromotionalFrontPage(){
      
		$html = "";
		if($_GET['sector']){
		$html .= "<a href=\"index.php?act=displaycurrentcategories&sector=".$_GET['sector']."\">Current Categories</a><br/>\n";
		$html .= "<a href=\"index.php?act=addcategory&sector=".$_GET['sector']."\">Add Categories</a><br/>\n";
		}
      
		echo($html);
      }
      
      
      function editSinglePromotion(){
      	require_once("core/sector.class.php");
      	require_once("db/dbconnect.class.php");
      	require_once("db/dbPromo.class.php");
      	
      	require_once("display/adminposdisplay.class.php");
      	$POSDISPLAY = new Adminposdisplay();
      	
      	$DB = new DbPromo();
      	$result = $DB->selectPromo($_GET['promoId']);
      	$promo = mysql_fetch_object($result);

      	$html .= "<form method=\"POST\" action=\"addextras.php\" onsubmit=\"return validate_form(this);\">";
		$html .= "<input type=\"hidden\" id=\"promoId\" name=\"promoId\" value=\"".$promo->id."\">\n";
		$html .= "<input type=\"hidden\" id=\"sector\" name=\"sector\" value=\"".$promo->sector."\">\n";
		$html .= "<input type=\"hidden\" id=\"category\" name=\"category\" value=\"".$promo->category."\">\n";
		$html .= "<div id=\"promoBasicHead\" style=\"position:relative;\">";
		$html .= "<p>";
        $html .= "<label for=\"nameOfPromotion\" class=\"promotionalNameLabel\">Name of Promotion </label>";
        $html .= "<input type=\"text\" name=\"nameOfPromotion\" id=\"nameOfPromotion\" value=\"".$promo->nameOfPromotion."\"/>";
		$html .= "<input type=\"checkbox\" name=\"frontPageYes\" checked />Display on the front page";
		$html .= "</p>";
		$html .= "<hr/>\n";
		$html .= "<label class=\"promotionalNameLabel\">Number of Images </label><input name=\"numberOfImages\" type=\"text\" id=\"numberOfImages\" size=\"2\" value=\"".count(explode(";",$promo->promotionalImages))."\"/>\n";
		$html .= "<input type=\"checkbox\" name=\"dontChangeImages\" checked />Don't change the images";
		$html .= "<hr/>\n";

		$html .= "<div id=\"posItems\">\n";
		$html .= $POSDISPLAY->displayCurrentPosEditForm($promo);
		$html .= "</div>\n";
		
      	
      	$html .= "<div id=\"submitsection\">\n";
		$html .= "<button type=\"submit\" id=\"Submit\" value=\"submit\">Submit</button>\n";
		$html .= "</div>\n";
      	
      	
      	$html .= "</div>\n";
      	$html .= "</form>";
      	
      	echo ( $html );
      	
      }
      
      // This thing is so uncommented, this comment will be all alone, sad comment :(
      function deleteSinglePromotion($promoid) {
      
      	require_once "config/config.php";
      	require_once "db/dbconnect.class.php";
      
      	// Instantiate a db class.
      	$db = new Connection();
      
      	$sql = "DELETE FROM `promotions` WHERE `id` = '".$promoid."'";
      	
      	mysql_query($sql) or die (mysql_error());
      	
      	header('location: index.php');
      
      }
      
      function displaySinglePromotion(){
      	require_once("core/sector.class.php");
      	require_once("db/dbconnect.class.php");
      	require_once("db/dbPromo.class.php");
      	
      	$DB = new DbPromo();
      	$result = $DB->selectPromo($_GET['promoId']);
      	$promo = mysql_fetch_object($result);
      	
      	$promotion = $this->createPromoObject($promo);
      	
      	$this->displayNameOfPromotion( $promotion->getPromotionName() );
      	$this->displayPromoImages( $promotion->getImages() );
      	$this->displayPromoItems( $promotion->getPromotionalItems() );
      	$this->displayPromoExtras( $promotion->getPromotionalExtras() );
      	}
      	
      function displayNameOfPromotion( $nameOfPromotion ) {
      	echo ("<h3>".$nameOfPromotion."</h3>");
      }
      function displayPromoImages($imagesArray){
      $html = "";
      	foreach ($imagesArray as $key => $val) {
      	 $html .= "<img src=\"http://127.0.0.1/aram/images/promoimages/".$val->getImageName()."\" />";
      	}
      	echo ($html);
      }
      function displayPromoItems($promoItems){
		$html = "<table>";
		$html .= "<tr>\n";
		$html .= "<td>Item</td>\n";
		$html .= "<td>Cost</td>\n";
		$html .= "<td>Extras</td>\n";
		$html .= "</tr>\n";
      		foreach ($promoItems as $key => $val) {
      			$html .= "<tr>\n";
      			$html .= "<td>".$val->getItemName()."</td>\n";
      			$html .= "<td>".$val->getItemCost()."</td>\n";
      			$html .= "<td>".$val->getextraInfo()."</td>\n";
      			$html .= "</tr>\n";
     	 }
      $html .= "</table>";
      echo ($html);
      }
      
      
      
      
      function displayPromoExtras ($array){
      if (!$array){ return false; }
      echo ("<div>\n");
      if (array_key_exists('writeDays', $array)) { echo "<p>Available Days: ".$array['writeDays'][0]."</p>"; }
	  if (array_key_exists('pricePoint', $array)) {echo "<p>PRICE POINT - Start Price: ".$array['pricePoint'][0]." End Price: ".$array['pricePoint'][1]." Interval: ".$array['pricePoint'][2]."</p>"; }

      echo ("</div>\n");
      }
      
      
      
      
      function createPromoObject($p){
     
      	require_once("core/promotion.class.php");
      	require_once("core/promotionalItem.class.php");
      	require_once("core/promotionalImage.class.php");
      	$pr = new Promotion();
      	$pr->setPromotionName($p->nameOfPromotion);
      	$pr->setSector($p->sector);
      	$pr->setCategory($p->category);
      	$pr->addImages($p->promotionalImages, $p->id);
      	$pr->addPromotion($p->promotionalItems);
      	$pr->addExtras($p->extras);
      	return $pr;
      
      }
      
      
      
      function displayAllPromotions(){
      	require_once("core/sector.class.php");
      	require_once("db/dbconnect.class.php");
      	require_once("db/dbPromo.class.php");
      	require_once("db/dbCategory.class.php");
      	$CDB = new DbCategory();
      	$DB = new DbPromo();
      	
      	$result = $CDB->selectCategory($_GET['id']);
      	$array = mysql_fetch_array($result);
      	$category =  $array['nameOfCategory'];
      	
      	$result = $DB->selectPromotions( $_GET['sector'], $_GET['id'] );
      	$html = "<h3>Current Promotions for ".$category."</h3> ";
      	while ($array = mysql_fetch_array($result) ){
      		$html .=  "<p>".$array['nameOfPromotion']." | <a href=\"index.php?act=editpromo&promoId=".$array['id']."\">Edit</a> | <a href=\"index.php?act=deletepromo&promoid=".$array['id']."\">Delete</a></p>";
      	}
      	echo ($html);
      }
      
      
      
      function displayCurrentPromotions(){
      		
      }
      
      
      
      
      
      function addPromotion(){
      	require_once("core/sector.class.php");
      	require_once("db/dbconnect.class.php");
      	require_once("db/dbPos.class.php");
      	require_once("display/adminposdisplay.class.php");
      	$POSDISPLAY = new Adminposdisplay();
      	$sectorId = $_GET['sector'];
      	$categoryId = $_GET['id'];
      	$SECTOR = new Sector();
		$array = $SECTOR->getSectors($sectorId);
      	$DB = new DbPos();
      	$DB->connectToServer();
      	require_once ("core/category.class.php");
     	$CATEGORY = new Category();
     	$categories = $CATEGORY->getIndividualCategory($categoryId);
     	     	$html = "<div class=\"form-container\">";
		foreach ($categories as $array){
		        $html .= "<h2>Add a <strong>".$array['nameOfCategory']."</strong> Promotion</h2>\n";
			}
			
			$html .= "<script type=\"text/javascript\">";
			$html .= "$(function() {";
			$html .= "$(\"#startDate\").datepicker({dateFormat: 'dd-mm-yy'});";
			$html .= "$(\"#endDate\").datepicker({dateFormat: 'dd-mm-yy'});";
			$html .= "$(\"#targetDate\").datepicker({dateFormat: 'dd-mm-yy'});";
			$html .= "});";
			$html .= "</script>";

		$html .= "<form method=\"post\" action=\"addextras.php\" onsubmit=\"return validate_form(this);\">";
		$html .= "<input type=\"hidden\" id=\"sector\" name=\"sector\" value=\"".$sectorId."\" />\n";
		$html .= "<input type=\"hidden\" id=\"category\" name=\"category\" value=\"".$categoryId."\" />\n";
		$html .= "<div id=\"promoBasicHead\" style=\"position:relative;\">";
		$html .= "<p>";
		$html .= "<label for=\"nameOfPromotion\" class=\"promotionalNameLabel\">Name of Promotion </label>";
		$html .= "<input type=\"text\" name=\"nameOfPromotion\" id=\"nameOfPromotion\" />";
		$html .= "</p>";
		
		$html .= "<p>Please input a start date: <input id=\"startDate\" name=\"startDate\" type=\"text\"/></p>";
		$html .= "<p>Please input a end date: <input id=\"endDate\" name=\"endDate\" type=\"text\"/></p>";
		$html .= "<p>Please input a target date: <input id=\"targetDate\" name=\"targetDate\" type=\"text\"/></p>";
		$html .= "<input type=\"checkbox\" name=\"frontPageYes\" checked>Display on the front page";
		
		$html .= "<table class=\"ds_box\" cellpadding=\"0\" cellspacing=\"0\" id=\"ds_conclass\" style=\"display: none;\">";
		$html .= "<tr><td id=\"ds_calclass\">";
		$html .= "</td></tr>";
		$html .= "</table>";


		$html .= "<hr/>\n";
		$html .= "<label class=\"promotionalNameLabel\">Number of Images </label><input name=\"numberOfImages\" type=\"text\" id=\"numberOfImages\" size=\"2\" />\n";
		$html .= "<hr/>\n";
		$html .= $this->buildSubCategorySelection($sectorId, $DB);
		$html .= "<hr/>\n";
		$html .= "</div>\n";
		
		
      	$html .= "<div id=\"promoBasicInfo\" style=\"position:relative;\">";
		// BUILD THE EXTRA FUNCTIONS SECTION
		$html .= "<fieldset><legend>Extra Items</legend>";

		$sql = "SELECT * FROM `extraFunctions`";
		$result = $DB->selectQuery("SELECT * FROM `extraFunctions`");
		while($array = mysql_fetch_array($result)){
		//echo $array['functionDisplayName'];
		$html .= "<div class=\"extraSection\">\n";
		$html .= "<p class=\"formSelection\"><label for=\"".$array['nameOfFunction']."\">".$array['functionDisplayName']."</label><input name=\"".$array['nameOfFunction']."\" id=\"".$array['nameOfFunction']."\" type=\"checkbox\"/></p>\n";
		$html .= "<p class=\"formDescription\">".$array['description']."</p><br/>\n";
		$html .= "</div>\n";
		}
		$html .= "</fieldset>\n";
		$html .= "</div>\n";
		// POS ITEMS
		
		
		
		$html .= "<div id=\"posItems\">\n";
		$html .= $POSDISPLAY->displayCurrentPosForForm();
		$html .= "</div>\n";
		
		$html .= "<div id=\"submitsection\">\n";
		$html .= "<button type=\"submit\" id=\"Submit\" value=\"submit\">Submit</button>\n";
		$html .= "</div>\n";
		$html .= "</form>";
		
		$html .= "</div>\n";
		echo($html);
      }
      
      function buildSubCategorySelection($sectorId, $DB){
      $result = $DB->selectQuery("SELECT * FROM `subcategories` WHERE `sectionID` = ".$sectorId."");
     
      	$html .= "<label class=\"promotionalNameLabel\">Sub Category </label>";
      	$html .= "<select id=\"subcategories\" name=\"subcategories\">\n";
      	$html .= "<option value=\"none\">No Sub Category</option>\n";
      	$html .= "<option value=\"new\">Add Sub Category</option>\n";
      	 while($array = mysql_fetch_array($result)){
		$html .= "<option label=\"".$array['title']."\" value=\"".$array['id']."\">".$array['title']."</option>\n";
		}
		$html .= "</select>\n";
		return $html;

		}
      
      function displaySectorsInOption(){
      require_once("core/sector.class.php");
      $SECTOR = new Sector();
      $array = $SECTOR->getSectors();
      $html = $this->buildSelectInput($array, 'id', 'nameOfSector');
      return $html;
      }
        
   	
}

?>